package com.iseastar.dianxiaosan.coupon;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iseastar.dianxiaosan.model.CouponBean;
import com.iseastar.dianxiaosan.stamp.StampStoreActivity;
import com.iseastar.guojiang.logic.AppLogic;
import com.shouhuobao.station.R;

import java.text.DateFormat;
import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.utils.android.Log;
import droid.frame.utils.lang.Str;
import droid.frame.utils.sqlite.DateUtils;

/**
 * 邮票
 *
 * @author coffee<br>
 *         2015-7-3上午9:46:28
 */
public class CouponListAdapter extends BaseAdapterExt<CouponBean> {
    /**
     * 当前正在使用的ID
     */
    private int selectId;

    private DateFormat sdf;

    private boolean showFooter = false;

    private int fontColor1, fontColor2;

    public CouponListAdapter(ArrayList<CouponBean> items, Activity context) {
        super(items, context);
        sdf = DateUtils.getDateFormat("yyyy-MM-dd");
        fontColor2 = context.getResources().getColor(R.color.app_font_2);
        fontColor1 = context.getResources().getColor(R.color.app_font_1);
        //
        if (context instanceof CouponListActivity) {
            setEmptyView(R.layout.coupon_empty);
        } else if (context instanceof CouponExpireListActivity) {
            setEmptyView(R.layout.coupon_expire_empty);
        }
    }

    public int getSelectId() {
        return selectId;
    }

    public void setSelectId(int selectId) {
        this.selectId = selectId;
    }

    /**
     * 是否显示底部Footer
     *
     * @param showFooter
     */
    public void setShowFooter(boolean showFooter) {
        this.showFooter = showFooter;
    }

//	@Override
//	public int getCount() {
//		return super.getCount() + (showFooter ? 1 : 0);
//	}

    private OnClickListener clickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(context, StampStoreActivity.class);
            context.startActivity(intent);
        }
    };

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            View layout = super.getView(position, convertView, parent);
            // 查看失效优惠卷
            View click = layout.findViewById(R.id.coupon_expired_click_1);
            if (click != null) {
                click.setOnClickListener(clickListener);
            }
            return layout;
        }
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.coupon_item, parent, false);
            holder = new ViewHolder();
            holder.layout1 = convertView.findViewById(R.id.coupon_layout_1);
            holder.layout2 = (ImageView) convertView.findViewById(R.id.coupon_state_image);
            //
            holder.useCondition = (TextView) convertView.findViewById(R.id.stamp_use_conditions);
            // 邮票--名称
            holder.icon = (ImageView) convertView.findViewById(R.id.coupon_icon);
            holder.name = (TextView) convertView.findViewById(R.id.stamp_name);
            holder.catagory = (TextView) convertView.findViewById(R.id.stamp_category);
            holder.deadline = (TextView) convertView.findViewById(R.id.stamp_deadline);
            holder.buttonstyle = (TextView) convertView.findViewById(R.id.coupon_button_style);
            //
            holder.footerLayout = (LinearLayout) convertView.findViewById(R.id.coupon_footer_layout);
            //
            holder.clickView2 = (TextView) convertView.findViewById(R.id.coupon_expired_click_2);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // footer
        Log.d("11111", getItemsSize());
        if (showFooter && position == getItemsSize() + (showFooter ? 1 : 0) - 1) {
            holder.layout1.setVisibility(View.GONE);
            holder.clickView2.setOnClickListener(clickListener);
            // empty
            if (position == 0 && getItemsSize() + (showFooter ? 1 : 0) == 1) {
                // SpannableString span = new SpannableString(str);
                // span.setSpan(new TextAppearanceSpan(context, R.style.app_button_base), 0, str1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                holder.footerLayout.setVisibility(View.GONE);
            } else {
                holder.footerLayout.setVisibility(View.VISIBLE);
            }
        } else {
            holder.layout1.setVisibility(View.VISIBLE);
            holder.footerLayout.setVisibility(View.GONE);

            CouponBean item = getItem(position);

            // 可用
            if (item.getIsUse() == 1) {
                // 选中
                if (selectId == item.getId()) {
                    holder.layout2.setImageResource(R.drawable.coupon_selected);
                    holder.layout2.setVisibility(View.VISIBLE);
                }
                // 正常
                else {
                    holder.layout2.setVisibility(View.GONE);
                }

                holder.catagory.setTextColor(fontColor1);
                holder.name.setTextColor(fontColor1);
                //
                holder.useCondition.setTextColor(fontColor2);
                holder.deadline.setTextColor(fontColor2);
            } else if (item.getIsUse() == 2 || item.getIsUse() == 3) {// 已使用 --过期
                holder.buttonstyle.setVisibility(View.VISIBLE);
                if (item.getIsUse() == 2) {// 已使用
                    // holder.layout2.setImageResource(R.drawable.coupon_used);
                    holder.buttonstyle.setText("已使用");
                } else {
                    // holder.layout2.setImageResource(R.drawable.coupon_expired);
                    holder.buttonstyle.setText("已失效");
                }
                holder.catagory.setTextColor(fontColor2);
                holder.name.setTextColor(fontColor2);
                holder.useCondition.setTextColor(fontColor2);
                holder.deadline.setTextColor(fontColor2);
            }
            if (item != null && !Str.isEmpty(item.getImage())) {
                AppLogic.loadImage(item.getImage(), holder.icon);
            }
            holder.name.setText(item.getName());
            holder.catagory.setText(item.getSeries());
            holder.useCondition.setText("使用条件:" + item.getCondition());
            holder.deadline.setText("有效期至" + item.getExpireDate());
        }
        return convertView;
    }

    private class ViewHolder {
        private View layout1;
        private ImageView layout2;// 选中、过期、已使用
        private ImageView icon;
        private TextView name; // 邮票名称
        private TextView useCondition; // 邮票描述
        private TextView deadline;// 有效期至
        private TextView catagory;
        private TextView buttonstyle;// 已失效 已使用
        private View footerLayout;
        private View clickView2;
    }

}
